package org.ascollada.core
{
	import org.ascollada.ASCollada;
	import org.ascollada.core.log.DaeLogger;
	
	public class DaeNode extends DaeElement
	{
		public static const TYPE_NODE	:String = "NODE";
		public static const TYPE_JOINT	:String = "JOINT";
		
		/** */
		public var type			:String;
		
		/** */
		public var nodes		:Array;
		
		/** */
		public var transforms	:Array;
		
		/** */
		public var controllers	:Array;
		
		/**
		 * 
		 */ 
		public function DaeNode(document:DaeDocument, node:XML=null)
		{
			super(document, node);
		}
		
		/**
		 * Reads the node.
		 * 
		 * @param	node
		 */ 
		public override function read( node:XML ):void
		{
			if ( !validateElement(node.name().localName) )
			{
				DaeLogger.error("Not a " + ASCollada.DAE_NODE_ELEMENT + " element!");
				throw new Error("Not a " + ASCollada.DAE_NODE_ELEMENT + " element!");
			}
			
			super.read( node );
			
			this.type = node.@type.toString();
			this.type = this.type.toUpperCase();
			this.type = this.type.indexOf(TYPE_JOINT) != -1 ? TYPE_JOINT : TYPE_NODE;
			
			this.nodes = new Array();
			this.controllers = new Array();
			this.transforms = new Array();
			this._transformByID = new Object();
			this._transformBySID = new Object();
			
			var children:XMLList = node.children();
			var numChildren:int = children.length();
			
			for(var i:int = 0; i < numChildren; i++)
			{
				var child:XML = children[ i ];
				
				switch( child.name().localName )
				{
					case ASCollada.DAE_MATRIX_ELEMENT:
					case ASCollada.DAE_ROTATE_ELEMENT:
					case ASCollada.DAE_TRANSLATE_ELEMENT:
					case ASCollada.DAE_LOOKAT_ELEMENT:
					case ASCollada.DAE_SCALE_ELEMENT:
						var transform : DaeTransform = new DaeTransform( this.document, child );
						this.transforms.push( transform );
						if( transform.id != "" )
							this._transformByID[ transform.id ] = transform;
						if( transform.sid != "" )
							this._transformBySID[ transform.id ] = transform;
						break;
						
					case ASCollada.DAE_NODE_ELEMENT:
						this.nodes.push( new DaeNode(this.document, child) );
						break;
					
					case ASCollada.DAE_INSTANCE_NODE_ELEMENT:
						this.nodes.push( new DaeInstanceNode(this.document, child) );
						break;
						
					case ASCollada.DAE_INSTANCE_CONTROLLER_ELEMENT:
						var instanceController : DaeInstanceController = new DaeInstanceController( this.document, child );
						this.controllers.push( instanceController );
						break;
							
					default:
						break;
				}
			}
		}
		
		/**
		 * Validate the element to be read.
		 * 
		 * @param	nodeName
		 * 
		 * @return
		 */ 
		public override function validateElement( nodeName:String ) : Boolean
		{
			return (nodeName == ASCollada.DAE_NODE_ELEMENT);
		}
		
		/**
		 * 
		 */ 
		public function getTransformByID( sid : String ) : DaeTransform
		{
			return _transformByID[ sid ];
		}
		
		/**
		 * 
		 */ 
		public function getTransformBySID( sid : String ) : DaeTransform
		{
			return _transformBySID[ sid ];
		}
		
		/** */
		private var _transformByID	:Object;
		
		/** */
		private var _transformBySID	:Object;
	}
}